package pathFinder.bfs;


import java.util.ArrayList;

public class Graph {

	private static ArrayList<Node> nodeList = new ArrayList<Node>();
	private static ArrayList<Edge> edgesList = new ArrayList<Edge>();
	private BFS bfs = new BFS();

	public  Node getAirpotNode(String airportName){
		
		for(int i = 0; i < nodeList.size(); ++i){
			if(nodeList.get(i).name.equals(airportName))
				return  nodeList.get(i);
		}
		// for now if not exist force creation
		Node newNodeAdded= new Node(airportName);
		nodeList.add(newNodeAdded);
		return newNodeAdded;
	}
	public  String getAirpotName(int id){
		
		for(int i = 0; i < nodeList.size(); ++i){
			if(nodeList.get(i).id_airport ==id)
				return nodeList.get(i).name;
			
		}

		return "nuexista";
	}
	public void addFlight(Edge e){
		edgesList.add(e);
		//System.out.println(e.from+"-"+e.to);
		getAirpotNode(e.from);
		getAirpotNode(e.from).connectionList.add(e);
		getAirpotNode(e.to);
	}
	
	public  void printGraph(){
		System.out.print("Nodes: ");
		for(int i = 0; i < nodeList.size(); ++i){
			System.out.print(nodeList.get(i).name+", ");
		}
		System.out.println("\nWith flights: ");
		for(int i = 0; i < edgesList.size(); ++i){
			Edge  e = edgesList.get(i);
			System.out.println(e.from+"->"+e.to+":"+e.getDepartureDay()+"->"+e.getHourDeparture()+":00+"+e.getDurationFlight()+"h");
			
		}
	}
	
	public String[] getBFSPaths(String fromAirport, String to, int maxDepth){
		ArrayList<Path>  allPaths = bfs.calcAllPaths(this,fromAirport,to,maxDepth);
		
		if(allPaths == null || allPaths.size() ==0){
			String []result = new String[2];
			result[0] = "No flights available please check parameters!\n";
			result[1] = "-1";
			return result;
		}
		float mindistance = allPaths.get(0).distance;
		int idminim = 0;
		
		for( int i = 1; i< allPaths.size(); i++){
				//System.out.println(allPaths.get(i).toString());
				if(mindistance < allPaths.get(i).distance){
					mindistance =allPaths.get(i).distance;
					idminim =  i;
				}
		}
		Path bestPath = allPaths.get(idminim);
		String []result = new String[bestPath.getNrConnections()+1];
		result[0] = bestPath.toString();
		for(int i = 0; i<bestPath.getNrConnections(); i++){
			
			result[i+1] = bestPath.fightIdList.get(i).toString();
		}
		return result;
	}
}
